Inheritance

Generalization vs. Specialization

Real-life objects are typically specialized versions of general objects.

The “is a” Relationship

Relationship between a superclass and an inherited class.

A specialized object has all the characteristics of the general object + additional characteristics that make it special.

Extending a class (extends) keyword

public class Grasshopper extends Insect

TO SHOW INHERITANCE IN UML, USE AN ARROW

Inheritance, Fields, and Methods

Members of the superclass that are private:

Members of the superclass that are public:

Inheritance and Constructors

Constructors aren’t inherited.

The Superclass’s Constructor

The super keyword refers to an object’s superclass (reference variable) and can be used to access members of the superclass.

Overriding Superclass Methods

Method Overriding: When a subclass has a method with the same signature as a superclass method, the subclass method overrides the superclass method.

Remember:

  1. Overriding can only happen in an inheritance relationship.
  2. Overloading is when methods share the same name but different parameters (different signatures)—don’t confuse it with overriding!

Preventing a Method from Being Overridden

final modifier prevents overriding of a superclass method.

public final void message()

Abstract Classes and Methods

public abstract class ClassName

An Abstract class:

AccessSpecifier abstract ReturnType Methodname(ParameterList);

Abstract classes are drawn like regular class in UML, except the name of the class and abstract methods are italicized.

Non-abstract classes are called concrete classes.

Protected Members

Third access specification: protected

BankAccountscore : doublesetScore(s : double) : voidgetScore() : voidgetGrade() : char

Default Access Modifier

If you don’t provide an access specifier for a class member, the member is given package access by default, meaning that any method in the same package may access the member.

BankAccountscore : doublesetScore(s : double) : voidgetScore() : voidgetGrade() : char

Chains of Inheritance

A superclass can inherit from another class.

The Object Class

All Java classes are directly or indirectly derived from a class named Object in the java.lang package.

Thus, every class inherits the Object class’s members (which we often override):

Polymorphism

Polymorphism: Ability to take many forms.

A superclass reference variable can reference objects of a subclasses.

GradedActivity exam;
  1. We can use the exam variable to reference a GradedActivity object
exam = new GradedActivity();
  1. Or we can create a reference to a FinalExam object
GradedActivity exam = new FinalExam(50,7);

Polymorphism and Dynamic Binding

When a superclass variable references a subclass object that has overridden a method in the superclass, the subclass’s version will be run when it’s called.

Java performs dynamic binding or late binding when a variable contains a polymorphic reference.

The instanceof Operator

instanceof can be used to determine whether an object is an instance of a particular class.

FinalExam exam = new FinalExam(20,2);
if (exam instanceof GradedActivity)
    // Does run
else
    // Doesn't run